﻿import odal.*;
import mx.xpath.XPathAPI;

class odal.OMSXmlDatasource extends OMSDatasource {
	private var m_omsStructure:OMSStructure;
	private var m_aRelationInfos:Array = new Array();
	
	public function OMSXmlDatasource( p_xmlfile:String ) {
//		trace( 'OMSXmlDatasource instantiated' );
		var xmlStructure:XML = new XML();
		this.m_datasource = xmlStructure;
		xmlStructure.load( p_xmlfile );
		xmlStructure.onData = function ( src:String ) {
//			trace( '************************[OMSXmlDataSource] BEGIN onData-inline-function' );
			if ( src == undefined ) {
//				trace( '[OMSXmlDataSource] src = undefined' );
				this.onLoad( false );
				this.m_datasource = null;
			} else {
//				trace( '[OMSXmlDataSource] src = ' + src );
				this.parseXML( src );
				this.loaded = true;
				trace( '[OMSXmlDataSource] vor onLoad.');
				this.onLoad( true );
			}//if
//			trace( '************************[OMSData.xmlDataSource] EMD onData-inline-function' );
		}//onData
	}//OMSXmlDatasource

	private function addData2Structure( p_omsStructure:OMSStructure ):Void {
//		trace ( '[OMSDatasource.addData2Stucture]' );
		var xml:XML = XML( this.m_datasource );
//		trace( xml.toString() );
		this.recurseElements( p_omsStructure, xml.childNodes );
	}//addData2Stucture
	
	private function recurseElements( p_omsStructure:OMSStructure, p_childNodes:Array ):Void {
//		trace( 'p_childNodes.toString(): ' + p_childNodes.toString() );
//		trace( 'p_childNodes.length: ' + p_childNodes.length );
		var childNode:XMLNode;
		var index:Number;
		var assSource:String;
		for ( index in p_childNodes ) {
			//trace( 'childNode.toString():' + childNode.toString() );
			childNode = p_childNodes[ index ];
			if ( ( childNode.nodeType === 1 ) && ( childNode.nodeName === 'Element' ) ) {
				this._addElement2Structure( p_omsStructure, childNode );
			}//if
			if ( ( childNode.nodeType === 1 ) && ( childNode.nodeName === 'Association' ) ) {
				this._addRelationInfo( 'a', childNode.parentNode.attributes.id, childNode.attributes.ElementId, childNode.attributes.attribute, 
									childNode.attributes.ass_id, childNode.attributes.key );
			}//if
			if ( childNode.hasChildNodes() ) {
				this.recurseElements( p_omsStructure, childNode.childNodes );
			}//if
		}//for
	}//recurseElements
	
	private function _addElement2Structure( p_omsStructure:OMSStructure, p_childNode:XMLNode ):Void {
		var element:OMSElement = new OMSElement( p_omsStructure, 
												 p_childNode.attributes.id,
												 p_childNode.attributes.name,
												 p_childNode.attributes.bookmark, 
												 p_childNode.attributes.scm_name, 
												 p_childNode.attributes.nsp_path );
		element.ParentElementId = p_childNode.attributes.parentElement;
		p_omsStructure.addElement( element );
		this._addRelationInfo( 'c', p_childNode.attributes.parentElement, element.Id, p_childNode.attributes.attribute, 
								p_childNode.attributes.cmp_id, p_childNode.attributes.key );
		this._addValues2Element( element, p_childNode );
	}//_addElement2Structure
	
	private function _addValues2Element( p_element:OMSElement, p_elementNode:XMLNode ):Void {
		var childNode:XMLNode;
		var childNodes:Array = p_elementNode.childNodes;
		var index:Number;
		for ( index in childNodes ) {
			//trace( 'childNode.toString():' + childNode.toString() );
			childNode = childNodes[ index ];
			if ( ( childNode.nodeType === 1 ) && ( childNode.nodeName === 'Value' ) ) {
				p_element.createValue( childNode.attributes.sat_name, 
									   childNode.attributes.den_name, 
									   childNode.attributes.id, 
									   childNode.attributes.value 
									 );
			}//if
		}//for
	}//_addValues2Element

	private function addRelations2Structure( p_omsStructure:OMSStructure ) {
//		trace ( '[OMSDatasource.addRelations2Structure] Anzahl Relationen: ' + this.m_aRelationInfos.length  );
		var relInfo:Object;
		var targetElement:OMSElement;
		var sourceElement:OMSElement; 
		for ( var index:String in this.m_aRelationInfos ) {
			relInfo = this.m_aRelationInfos[ index ];
//			trace ( '[OMSDatasource.addRelations2Structure] type: ' + relInfo.type + ' - source: ' + relInfo.source + ' - target: ' + relInfo.target );
			sourceElement = p_omsStructure.getElementById( relInfo.source );
			targetElement = p_omsStructure.getElementById( relInfo.target );
			if ( targetElement != null ) {
				if ( sourceElement == null ) {
					sourceElement = p_omsStructure.Root;
				}//if 
				//trace ( '[OMSDatasource.addRelations2Structure] sourceId: ' + sourceElement.Id + ' - targetId: ' + targetElement.Id );
				sourceElement.relate( targetElement, relInfo.catName, relInfo.type, relInfo.relId, relInfo.key )
			}//if
		}//for
	}//addRelations2Structure

	private function _addRelationInfo( type:String, source:String, target:String, catName:String, relId:String, key:String ) {
		var relInfo = new Object();
		relInfo.type = type;
		relInfo.source = source;
		relInfo.target = target;
		relInfo.catName = catName;
		relInfo.relId = relId;
		relInfo.key = key;
		this.m_aRelationInfos.push( relInfo );
	}//_addRelationInfo
}//odal.OMSXmlDatasource